package com.zy.controller;

import com.zy.entity.ZyMenu;
import com.zy.entity.ZyRole;
import com.zy.mapper.ZyRoleMapper;
import com.zy.security.UserMenuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
@Slf4j
public class AccessMenuController {
    @Autowired
    private UserMenuUtils userMenuDetails;
    @Autowired
    private ZyRoleMapper zyRoleMapper;

    /**
     * 获取当前用户所拥有的菜单项
     * @param authentications
     * @return
     */
    @RequestMapping("/menu")
    public ModelAndView showMenuByUser(Authentication authentications) {
        log.info("登录的用户：{}",SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        ModelAndView modelAndView = new ModelAndView();
        String username = authentications.getName();
        //根据权限，获取当前用户所拥有的菜单项
        List<ZyMenu> userMenu = userMenuDetails.getUserMenu(username);
        modelAndView.addObject("manages", userMenu);
        modelAndView.setViewName("manage");
        return modelAndView;
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/roles")
    @ResponseBody
    public List<ZyRole> getRoles() {

        //查询所有角色
        List<ZyRole> zyRoles = zyRoleMapper.selectList(null);
        return zyRoles;
    }
}
